{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ec120a00",
   "metadata": {},
   "source": [
    "# 本周知识  \n",
    "\n",
    "1. 函数的返回值\n",
    "2. 函数的输入参数类型\n",
    "3. 函数的返回参数类型\n",
    "4. 模块的概念\n",
    "5. 如何创建一个模块，并调用自己定义的模块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9196ab02",
   "metadata": {},
   "source": [
    "## 函数的返回值\n",
    "\n",
    "> 1. 返回值的关键词：return\n",
    ">> 1. 函数一定有返回值（无论是什么编程语言）\n",
    ">> 2. 在其他的编程语言中（如C语言，java等），返回值一定要写，不写会报错！；对于python（更高级的语言），返回值如果不写，给定默认值none\n",
    ">> 3. 返回值一般为函数体运行的结果（result）(函数返回值实践1)；也可以是自定义的函数返回值（实践2）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fbd6c33b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数的返回值实践1\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "#     print(full_name)\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce679e8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "get_formatted_name（'li','hua'）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "94e6b45a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "li hua\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name('li','hua'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c1c751af",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践2\n",
    "def log_In(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == 'lihua' and password ==123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "dd2eef3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_In('lihua',12345)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9d03d147",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_In('lihua',123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8724e199",
   "metadata": {},
   "source": [
    "## 函数的输入参数类型\n",
    "> 1. 函数可以输入复杂的参数；\n",
    ">> 1. 函数参数为列表\n",
    ">> 2. 函数参数为字典\n",
    ">> 3. ...\n",
    "\n",
    "> 2. 函数可以输入不确定个数的参数\n",
    ">> 1. *args\n",
    ">> 2. **kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "44379df0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_A(names):\n",
    "    \"\"\"测试参数输入列表\"\"\"\n",
    "    return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "25bc8f22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['LIHUA', 'ALEX', 'ZIQI']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_A(['LIHUA','ALEX','ZIQI'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a6247679",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_B(person):\n",
    "    \"测试函数参数输入为字典\"\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1d93ae4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu_id': 210001, 'age': 18, 'job': 'student'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_B({'stu_id':210001,\"age\":18,\"job\":\"student\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e971483",
   "metadata": {},
   "source": [
    "## 3. 函数的输出（返回值）参数类型\n",
    "\n",
    "> 1. 在编程语言中，函数的返回值有且只有一个\n",
    "> 2. 返回值可以是 字符串、数值、列表、字典等数据（单一值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "80236a8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    '''测试返回值return为字符串'''\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "24fe3309",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "7442c168",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return为数值\"\"\"\n",
    "    return 12345"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "bcdb23a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ca8288e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return为列表\"\"\"\n",
    "    return ['Lihua', 'Alex', 'Ziqi']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "8cb61c14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Ziqi']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "5554d184",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return为字典\"\"\"\n",
    "    return {'stud_id': 210001, 'age': 18, 'Job': 'student'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "c6259789",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "0f490115",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ******** return 后面写多个值，将会返回一个元组（一个值，不违背函数在编程思维中的定义）\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return 为 多个值的情况\"\"\"\n",
    "    return 18,\"Lihua\",['Lihua', 'Alex', 'Ziqi'],{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "900d90af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18,\n",
       " 'Lihua',\n",
       " ['Lihua', 'Alex', 'Ziqi'],\n",
       " {'stud_id': 210001, 'age': 18, 'Job': 'student'})"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "a5277871",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test6_return():\n",
    "    \"\"\"测试返回值return 为 多个值的情况\"\"\"\n",
    "    return 88,89,90,90"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "5b70aec4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(88, 89, 90, 90)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test6_return()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51234499",
   "metadata": {},
   "source": [
    "> 2. 函数可以输入不确定个数的参数：\n",
    ">> 1. *args\n",
    ">>> 1. args 可以接受多个数据（单值）\n",
    ">>> 2. args 将多个数据存在一个**元组**当中，实际还是一个变量\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "8c988f21",
   "metadata": {},
   "outputs": [],
   "source": [
    "def food_ingred(food,*ingreds):\n",
    "    \"\"\"食物配料单\"\"\"\n",
    "    return food,ingreds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "98cd31ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food_ingred('pizza','mushroom','green peppers','extra cheese')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "1e77fa0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def person(stu_name,**stud_course):\n",
    "    '''学生个人信息完善表'''\n",
    "    return stud_name,stud_course\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "b7a29deb",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'stud_name' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-49-c9fa7f429ec9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mperson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'lihua'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcourse_星期一\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'api'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcourse_星期二\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'app原型设计'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-48-fded3a8f5763>\u001b[0m in \u001b[0;36mperson\u001b[1;34m(stu_name, **stud_course)\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mperson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstu_name\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mstud_course\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[1;34m'''学生个人信息完善表'''\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mstud_name\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mstud_course\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'stud_name' is not defined"
     ]
    }
   ],
   "source": [
    "person('lihua',course_星期一='api',course_星期二='app原型设计')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57d6fb93",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "d1f6a0f9",
   "metadata": {},
   "source": [
    "## 模块\n",
    "\n",
    "> 1. 使用模块可以隐藏函数的源代码\n",
    "> 2. 使用的人不需要会底层代码（只需要使用模块即可）\n",
    "\n",
    "> 3. 模块的使用方法\n",
    ">> 1. import 模块名\n",
    ">>> * 使用：模块名.函数名（）\n",
    ">> 2. from 模块名 import 函数名\n",
    ">>> * 使用：函数名（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "eeb15ed8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "bf55a8ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "9c41ff13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 函数的调用\n",
    "study.search4vowels('xuzhicahoa')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "aed6faa3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function news_链接 in module study:\n",
      "\n",
      "news_链接(url, 新闻分类, page)\n",
      "    抓取学校新闻的所有页面链接\n",
      "    url:学校网站的域名链接\n",
      "    新闻分类:学校新闻的分类英文简称\n",
      "    page:你想要抓取的页面尾页\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(study.news_链接)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "cd8e7637",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (<ipython-input-56-b1c52da9ce6b>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-56-b1c52da9ce6b>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    study.news_链接（'https://www.nfu.edu.cn'）\u001b[0m\n\u001b[1;37m                 ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "study.news_链接（'https://www.nfu.edu.cn'）"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
